package com.df.bpm.utils.businessViewerV2.business;

import com.df.bpm.utils.businessViewerV2.Main;
import com.df.bpm.utils.businessViewerV2.entity.C_entity;
import com.df.bpm.utils.businessViewerV2.entity.M_entityDto;
import com.df.bpm.utils.businessViewerV2.res.V_scrollPanel;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class V_scroll4InputDto extends V_scrollPanel<M_businessInputDto> {

    V_businessByOutput baba;
    Integer activeBusiness;

    public V_scroll4InputDto(V_businessByOutput baba){
        this.baba=baba;
        this.putModel(null);
    }

    @Override
    public List<String> getColNames() {
        return Arrays.asList("输入物ID", "序号","实体ID","输入物名称","创建时间","更新时间");
    }

    @Override
    public void hideCol(JTable iTable) {
        Main.hideCol(iTable,0);
        Main.hideCol(iTable,2);
    }

    @Override
    public List<Integer> colWidthSetting() {
        return new ArrayList<>(Arrays.asList(1,3,1,11,11,11));
    }

    @Override
    public Object[] getRowValues(M_businessInputDto row) {
        return new Object[]{row.getBusinessInputsIndex(),-1,row.getBusinessEntityIndex(),
                row.getEntityName(),
                dateFormat.format(row.getCreateDate()),
                dateFormat.format(row.getUpdateDate())};
    }

    @Override
    public M_businessInputDto getEmptyRow() {
        return new M_businessInputDto();
    }

    @Override
    public MouseListener mouseAdapter4Table(JTable iTable, List<M_businessInputDto> list) {
        return new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int row = iTable.rowAtPoint(e.getPoint());
//                int col = iTable.columnAtPoint(e.getPoint());
                if (row >= 0 ) {
                    if (e.getClickCount() == 2) {// 处理双击事件
                        if(activeBusiness!=null){
                            M_entityDto mEntityDto = C_entity.selectOne();
                            if(mEntityDto!=null){
                                M_businessInputDto temp = list.get(row);
                                if(mEntityDto.getBusinessEntityIndex()==-1){//删除
                                    if(temp.getBusinessInputsIndex()!=null){
                                        C_business.deleteInput(temp.getBusinessInputsIndex());
                                    }
                                }else{//增、改
                                    if (!Objects.equals(temp.getBusinessEntityIndex(), mEntityDto.getBusinessEntityIndex())){//新增
                                        temp.setBusinessEntityIndex(mEntityDto.getBusinessEntityIndex());
                                        temp.setBusinessInformationIndex(activeBusiness);
                                        C_business.updateInput(temp);
                                    }
                                }
                                showBusiness(activeBusiness);
                            }
                        }
                    }
                }
            }
        };
    }

    public void showBusiness(Integer businessId) {
        activeBusiness = businessId;
        List<M_businessInputDto> inputDtos=C_business.getInputDtos(activeBusiness);
        putModel(inputDtos);//查输入物
    }
}
